<?php

namespace App\Services;

use App\Interfaces\ArticleInterface;
use Illuminate\Support\Facades\DB;

class ArticleService implements ArticleInterface
{

    public function getById(int $id)
    {
        $where = array(
            ['deleted',0],
            ['id',$id]
        );
        $res = DB::table('content_article')->where($where)->get()->first();
        return $res;
    }

    public function getAll()
    {
        // TODO: Implement getAll() method.
    }

    public function getPage(int $tenantId, array $params = [], int $page = 1, int $limit = 10): array
    {
        $where = array(
            ['deleted',0],
            ['tenantId',$tenantId],
            ['title','like','%'.$params['title'].'%'],
            ['cateId',$params['cateId']]
        );

        if (!empty($params['user']))
        {
            $where[] = ['user', 'like', '%' . $params['user'] . '%'];
        }

        $articles = DB::table('content_article')
            ->where($where)
            ->orderBy('createTime','DESC')
            ->offset($page -1)
            ->paginate($limit);
        $res['list'] = $articles->items();
        $res['count']= $articles->total();
        return $res;
    }

    public function addArticle(array $data): int
    {
        $res = DB::table('content_article')->insertGetId($data);
        return $res;
    }

    public function updateArticle(array $data): int
    {
        $res = DB::table('content_article')->where('id',$data['id'])->update($data);
        return $res;
    }
}
